package com.iflytek.universitymanagement.service.impl;

import com.iflytek.universitymanagement.domain.TutoringPlan;
import com.iflytek.universitymanagement.domain.TutoringPlanWithStudentName;
import com.iflytek.universitymanagement.mapper.TutoringPlanMapper;
import com.iflytek.universitymanagement.service.TutoringPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TutoringPlanServiceImpl implements TutoringPlanService {

    @Autowired
    private TutoringPlanMapper tutoringPlanMapper;

    @Override
    public List<TutoringPlanWithStudentName> getAllTutoringPlans() {
        return tutoringPlanMapper.getTutoringPlansWithStudentNames();
    }

    @Override
    public TutoringPlan getTutoringPlanById(Integer planId) {
        return tutoringPlanMapper.selectById(planId);
    }

    @Override
    public boolean createTutoringPlan(TutoringPlan tutoringPlan) {
        // 在创建辅导计划时，确保提供学生的 ID 和其他必要信息
        // 保存辅导计划到数据库
        return tutoringPlanMapper.insert(tutoringPlan) > 0; // 如果插入成功，返回 true
    }
    // 更新辅导计划
    @Override
    public boolean updateTutoringPlan(Integer planId, TutoringPlan tutoringPlan) {
        // 首先查找是否有该辅导计划
        TutoringPlan existingPlan = tutoringPlanMapper.selectTutoringPlanByPlanIdAndStudentId(planId, tutoringPlan.getStudentId());

        if (existingPlan == null) {
            // 如果没有找到该辅导计划，返回 false
            return false;
        }

        // 如果找到辅导计划，更新计划
        tutoringPlan.setPlanId(planId);  // 确保更新的是指定的计划
        return tutoringPlanMapper.updateById(tutoringPlan) > 0;  // 使用 MyBatis-Plus 的 updateById 更新
    }
    @Override
    public boolean deleteTutoringPlan(Integer planId) {
        return tutoringPlanMapper.deleteById(planId) > 0;
    }
}
